using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UWBGL_WinForms_Lib1;
using UWBGL_XNA_Lib13;

namespace XNA_SceneNode
{
    public class Model
    {
        private UWB_SceneNode mScene;
        private UWB_SceneNode mLeftPalmNode, mRightPalmNode;
        private UWB_SceneNode mActiveNode;


        private UWB_PrimitiveList mBullets;
        private float mSecondCounter;
        private int mHitCount;
				private int bulletFrequency;

        private UWB_XNADrawHelper mDrawHelper;
        private UWB_BoundingBox mWorldBounds;

        private UWB_Clock mStopWatch;

        // Variables that we added to make life easier
        //This is a pointer to our Net Node
        private UWB_SceneNode ScopeNetHolder;
        private UWB_SceneNode ScopeWithTarget;

        //Pointers to our target Nodes
        private UWB_SceneNode ArmWithTarget;
        private UWB_PrimitiveCircle ArmTargetPointer;
        private Vector3 ArmTargetWhere;

        private UWB_SceneNode ArmNetHolder;
        

        private bool AnimateArm = false;
        private int AnimateArmCount = 0;
        UWB_XFormInfo Animate_Old;

        private bool AnimateScope = false;
        private int AnimateScopeCount = 0;

        private UWB_SceneNode SubNode;

        public Model()
        {
					bulletFrequency = 5;
            mScene = new UWB_SceneNode("Scene");



            //UWB_SceneNode body = new UWB_SceneNode("Body");
            //mScene.insertChildNode(body);
            mActiveNode = mScene;

            //UWB_PrimitiveList bodyPrim = new UWB_PrimitiveList();
            //UWB_PrimitiveRectangle rec = new UWB_PrimitiveRectangle();
            //rec.setCorners(new Vector3(-1.0f, -5.0f, 0.0f), new Vector3(1.0f, 5.0f, 0.0f));
            //rec.setFlatColor(new Color(50, 50, 255));
            //rec.setShadeMode(eShadeMode.smFlat);
            //bodyPrim.append(rec);

            //UWB_PrimitiveCircle circle = new UWB_PrimitiveCircle();
            //circle.setCenter(0.0f, 0.0f, 0.0f);
            //circle.setRadius(1.75f);
            //circle.setFlatColor(new Color(0, 0, 100, 100));
            //circle.setShadeMode(eShadeMode.smFlat);
            //bodyPrim.append(circle);
            //body.setPrimitive(bodyPrim);

            //CArm l = new CArm(new Vector3(1.0f, 4.0f, 0.0f), "LeftArm");
            //CArm r = new CArm(new Vector3(1.0f, -4.0f, 0.0f), "RightArm");
            //mLeftPalmNode = l.GetPalmNode();
            //mRightPalmNode = r.GetPalmNode();
            //body.insertChildNode(l);
            //body.insertChildNode(r);

            //UWB_XFormInfo xf = body.getXFormInfo();
            //xf.setRotationDegrees(90.0f);
            //xf.setTranslation(new Vector3(0.0f, -6.0f, 0.0f));
            //body.setXFormInfo(xf);




            /////////////////////////////////////////////
            //Let's try and make a sub
            //Make a secen node for our sub & hook it up
            UWB_SceneNode Sub = new UWB_SceneNode("Sub");
            mScene.insertChildNode(Sub);

            //Make a primitive list
            UWB_PrimitiveList SubBody = new UWB_PrimitiveList();

            //Make the square body
            UWB_PrimitiveRectangle Box = new UWB_PrimitiveRectangle();
            Box.setCorners(new Vector3(2.0f, 0.0f, 0.0f), new Vector3(10.0f, 4.0f, 0.0f));
            Box.setFlatColor(new Color(255, 255, 00));
            Box.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Box);

            //Make the front circle
            UWB_PrimitiveCircle Front = new UWB_PrimitiveCircle();
            Front.setCenter(10.0f, 2.0f, 0.0f);
            Front.setRadius(2.0f);
            Front.setFlatColor(new Color(255, 255, 00, 0));
            Front.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Front);

            //Make the Wheel
            UWB_PrimitiveStar Wheel = new UWB_PrimitiveStar();
            Wheel.setCenter(1.0f, 1.0f, 0.0f);
            Wheel.setRadius(2.0f);
            Wheel.setFlatColor(new Color(10, 10, 10, 0));
            Wheel.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Wheel);

            //Make the back of the sub
            UWB_PrimitiveCircle Back = new UWB_PrimitiveCircle();
            Back.setCenter(2.0f, 2.0f, 0.0f);
            Back.setRadius(2.0f);
            Back.setFlatColor(new Color(255, 255, 00, 0));
            Back.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Back);

            //Make the neck
            UWB_PrimitiveRectangle Neck = new UWB_PrimitiveRectangle();
            Neck.setCorners(new Vector3(4.0f, 4.0f, 0.0f), new Vector3(8.0f, 8.0f, 0.0f));
            Neck.setFlatColor(new Color(255, 255, 00));
            Neck.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Neck);
            Sub.setPrimitive(SubBody);

            //Make the Window
            UWB_PrimitiveCircle Window = new UWB_PrimitiveCircle();
            Window.setCenter(6.0f, 6.0f, 0.0f);
            Window.setRadius(1.0f);
            Window.setFlatColor(new Color(00, 255, 255, 0));
            Window.setShadeMode(eShadeMode.smFlat);
            SubBody.append(Window);

            SubNode = Sub;

            //Ok done making the body of the sub
            ///////////////////////////////////

            //Let's make the Neck of the scope
            //Make a scene node for our ScopeNeck & hook up
            UWB_SceneNode ScopeNeck = new UWB_SceneNode("Scope Neck");
            Sub.insertChildNode(ScopeNeck);

            //Make a primitive list
            UWB_PrimitiveList ScopeList = new UWB_PrimitiveList();

            //Make the square ScopeNeck
            UWB_PrimitiveRectangle SNeck = new UWB_PrimitiveRectangle();
            SNeck.setCorners(new Vector3(5.0f, 8.0f, 0.0f), new Vector3(7.0f, 12.0f, 0.0f));
            SNeck.setFlatColor(new Color(00, 00, 00));
            SNeck.setShadeMode(eShadeMode.smFlat);
            ScopeList.append(SNeck);
            

            //Make a target       
            UWB_PrimitiveCircle ScopeTarget = new UWB_PrimitiveCircle();
            ScopeTarget.setCenter(6.0f, 9.0f, 0.0f);
            ScopeTarget.setRadius(1.0f);
            ScopeTarget.setFlatColor(new Color(00, 00, 255, 0));
            ScopeTarget.setShadeMode(eShadeMode.smFlat);
            ScopeList.append(ScopeTarget);
            ScopeNeck.setPrimitive(ScopeList);

            ScopeWithTarget = ScopeNeck;

            UWB_XFormInfo SNxf = ScopeNeck.getXFormInfo();
            SNxf.setRotationDegrees(0.0f);
            SNxf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            SNxf.setPivot(new Vector3(6.0f, 8.0f, 0.0f));
            ScopeNeck.setXFormInfo(SNxf);

            //Now the actual Scope itself
            //Make a scene node for our Scope & hook up
            UWB_SceneNode Scope = new UWB_SceneNode("Scope");
            ScopeNeck.insertChildNode(Scope);

            //Make a primitive list
            UWB_PrimitiveList ScopeL = new UWB_PrimitiveList();

            //Make the square Scope
            UWB_PrimitiveRectangle ScopeRec = new UWB_PrimitiveRectangle();
            ScopeRec.setCorners(new Vector3(7.0f, 10.0f, 0.0f), new Vector3(11.0f, 12.0f, 0.0f));
            ScopeRec.setFlatColor(new Color(00, 00, 00));
            ScopeRec.setShadeMode(eShadeMode.smFlat);
            ScopeL.append(ScopeRec);


            //Make a Target
            UWB_PrimitiveCircle ScopeNetTarget1 = new UWB_PrimitiveCircle();
            ScopeNetTarget1.setCenter(12.0f, 12.0f, 0.0f);
            ScopeNetTarget1.setRadius(1.0f);
            ScopeNetTarget1.setFlatColor(new Color(255, 255, 255, 0));
            ScopeNetTarget1.setShadeMode(eShadeMode.smFlat);
            ScopeL.append(ScopeNetTarget1);

            //Make a Target2
            UWB_PrimitiveCircle ScopeNetTarget2 = new UWB_PrimitiveCircle();
            ScopeNetTarget2.setCenter(14.0f, 12.0f, 0.0f);
            ScopeNetTarget2.setRadius(1.0f);
            ScopeNetTarget2.setFlatColor(new Color(255, 255, 255, 0));
            ScopeNetTarget2.setShadeMode(eShadeMode.smFlat);
            ScopeL.append(ScopeNetTarget2);

            //Make a Target
            UWB_PrimitiveCircle ScopeNetTarget3 = new UWB_PrimitiveCircle();
            ScopeNetTarget3.setCenter(12.0f, 10.0f, 0.0f);
            ScopeNetTarget3.setRadius(1.0f);
            ScopeNetTarget3.setFlatColor(new Color(255, 255, 255, 0));
            ScopeNetTarget3.setShadeMode(eShadeMode.smFlat);
            ScopeL.append(ScopeNetTarget3);

            //Make a Target
            UWB_PrimitiveCircle ScopeNetTarget4 = new UWB_PrimitiveCircle();
            ScopeNetTarget4.setCenter(14.0f, 10.0f, 0.0f);
            ScopeNetTarget4.setRadius(1.0f);
            ScopeNetTarget4.setFlatColor(new Color(255, 255, 255, 0));
            ScopeNetTarget4.setShadeMode(eShadeMode.smFlat);
            ScopeL.append(ScopeNetTarget4);


            //Then we set the primitive
            Scope.setPrimitive(ScopeL);
            ScopeNetHolder = Scope;
            UWB_XFormInfo Sxf = Scope.getXFormInfo();
            Sxf.setRotationDegrees(0.0f);
            Sxf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            Sxf.setPivot(new Vector3(7.0f, 11.0f, 0.0f));
            Scope.setXFormInfo(Sxf);

            /////////////////////////////




            //We'll go one more level just in case
            //Make a scene node for our ScopeNet & hook up
            UWB_SceneNode ScopeNet = new UWB_SceneNode("Scope Net");
            Scope.insertChildNode(ScopeNet);

            //Make a primitive list
            UWB_PrimitiveList ScopeNetList = new UWB_PrimitiveList();

            //Make the square ScopeNet
            UWB_PrimitiveRectangle ScopeNetRec = new UWB_PrimitiveRectangle();
            ScopeNetRec.setCorners(new Vector3(11.0f, 9.0f, 0.0f), new Vector3(15.0f, 13.0f, 0.0f));
            ScopeNetRec.setFlatColor(new Color(255, 255, 255));
            ScopeNetRec.setShadeMode(eShadeMode.smFlat);
            ScopeNetList.append(ScopeNetRec);
            ScopeNet.setPrimitive(ScopeNetList);


            /////////////////////////////////
            //Now let's make the Grabbers & Net
            // We need to hook this up to the sub body
            //Make a scene node for our ScopeNeck & hook up
            UWB_SceneNode Arm = new UWB_SceneNode("Arm");
            Sub.insertChildNode(Arm);

            //Make a primitive list
            UWB_PrimitiveList ArmList = new UWB_PrimitiveList();

            //Make the square of Arm
            UWB_PrimitiveRectangle ArmRec = new UWB_PrimitiveRectangle();
            ArmRec.setCorners(new Vector3(10.0f, 1.0f, 0.0f), new Vector3(16.0f, 3.0f, 0.0f));
            ArmRec.setFlatColor(new Color(00, 00, 00));
            ArmRec.setShadeMode(eShadeMode.smFlat);
            ArmList.append(ArmRec);
            Arm.setPrimitive(ArmList);

            //Make a Target
            UWB_PrimitiveCircle ArmTarget = new UWB_PrimitiveCircle();
            ArmTarget.setCenter(11.0f, 2.0f, 0.0f);
            ArmTarget.setRadius(1.0f);
            ArmTarget.setFlatColor(new Color(255, 0, 0, 0));
            ArmTarget.setShadeMode(eShadeMode.smFlat);
            ArmList.append(ArmTarget);

            ArmTargetPointer = ArmTarget;
            ArmTargetWhere = new Vector3(11.0f, 2.0f, 0.0f);

            UWB_XFormInfo Axf = Arm.getXFormInfo();
            Axf.setRotationDegrees(0.0f);
            Axf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            Axf.setPivot(new Vector3(10.0f, 2.0f, 0.0f));
            Arm.setXFormInfo(Axf);

            ArmWithTarget = Arm;



            // Arm 2
            //Make a scene node for our Arm2 & hook up
            UWB_SceneNode LittleArm = new UWB_SceneNode("Little Arm");
            Arm.insertChildNode(LittleArm);

            //Make a primitive list
            UWB_PrimitiveList Arm2List = new UWB_PrimitiveList();

            //Make the square Arm2
            UWB_PrimitiveRectangle Arm2Rec = new UWB_PrimitiveRectangle();
            Arm2Rec.setCorners(new Vector3(16.0f, 1.0f, 0.0f), new Vector3(20.0f, 3.0f, 0.0f));
            Arm2Rec.setFlatColor(new Color(00, 00, 00));
            Arm2Rec.setShadeMode(eShadeMode.smFlat);
            Arm2List.append(Arm2Rec);
            LittleArm.setPrimitive(Arm2List);

            UWB_XFormInfo LAxf = LittleArm.getXFormInfo();
            LAxf.setRotationDegrees(0.0f);
            LAxf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            LAxf.setPivot(new Vector3(16.0f, 2.0f, 0.0f));
            LittleArm.setXFormInfo(LAxf);


            // Net
            //Make a scene node for our Net & hook up
            UWB_SceneNode ArmNet = new UWB_SceneNode("Arm Net");
            LittleArm.insertChildNode(ArmNet);

            //Make a primitive list
            UWB_PrimitiveList ArmNetList = new UWB_PrimitiveList();

            //Make the square ArmNet
            UWB_PrimitiveRectangle ArmNetRec = new UWB_PrimitiveRectangle();
            ArmNetRec.setCorners(new Vector3(20.0f, 0.0f, 0.0f), new Vector3(24.0f, 4.0f, 0.0f));
            ArmNetRec.setFlatColor(new Color(255, 255, 255));
            ArmNetRec.setShadeMode(eShadeMode.smFlat);
            ArmNetList.append(ArmNetRec);

            //Make a Target
            UWB_PrimitiveCircle ArmNetTarget1 = new UWB_PrimitiveCircle();
            ArmNetTarget1.setCenter(21.0f, 3.0f, 0.0f);
            ArmNetTarget1.setRadius(1.0f);
            ArmNetTarget1.setFlatColor(new Color(255, 255, 255, 0));
            ArmNetTarget1.setShadeMode(eShadeMode.smFlat);
            ArmNetList.append(ArmNetTarget1);

            //Make a Target2
            UWB_PrimitiveCircle ArmNetTarget2 = new UWB_PrimitiveCircle();
            ArmNetTarget2.setCenter(23.0f, 3.0f, 0.0f);
            ArmNetTarget2.setRadius(1.0f);
            ArmNetTarget2.setFlatColor(new Color(255, 255, 255, 0));
            ArmNetTarget2.setShadeMode(eShadeMode.smFlat);
            ArmNetList.append(ArmNetTarget2);

            //Make a Target
            UWB_PrimitiveCircle ArmNetTarget3 = new UWB_PrimitiveCircle();
            ArmNetTarget3.setCenter(21.0f, 1.0f, 0.0f);
            ArmNetTarget3.setRadius(1.0f);
            ArmNetTarget3.setFlatColor(new Color(255, 255, 255, 0));
            ArmNetTarget3.setShadeMode(eShadeMode.smFlat);
            ArmNetList.append(ArmNetTarget3);

            //Make a Target
            UWB_PrimitiveCircle ArmNetTarget4 = new UWB_PrimitiveCircle();
            ArmNetTarget4.setCenter(23.0f, 1.0f, 0.0f);
            ArmNetTarget4.setRadius(1.0f);
            ArmNetTarget4.setFlatColor(new Color(255, 255, 255, 0));
            ArmNetTarget4.setShadeMode(eShadeMode.smFlat);
            ArmNetList.append(ArmNetTarget4);;

            ArmNet.setPrimitive(ArmNetList);
            //ArmNetHolder = ArmNet;

            // We might need a xform info thing for our sub
            //This way we can positon, scale, etc. to make it look right.
            UWB_XFormInfo Subxf = Sub.getXFormInfo();
            Subxf.setRotationDegrees(0.0f);
            Subxf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            //Subxf.setScale(new Vector3(0.5f, 0.5f, 0.5f));
            Subxf.setScale(new Vector3(0.2f, 0.2f, 0.2f));
            Sub.setXFormInfo(Subxf);



            //
            ///////////////////////////////////////////
            // End of all the sub fun
            /////////////////////////////////////

            // Let's block these out just for now
            //// A couple of lines and a couple of points to show texture mapping on lines/points
            //UWB_SceneNode ln = new UWB_SceneNode("Line Node");
            //mScene.insertChildNode(ln);
            //UWB_PrimitiveList list = new UWB_PrimitiveList();
            //UWB_PrimitiveLine lines = new UWB_PrimitiveLine();
            //lines.setStartPoint(1, 1, 0);
            //lines.setEndPoint(5, 5, 0);
            //list.append(lines);
            //lines = new UWB_PrimitiveLine();
            //lines.setStartPoint(2, 5, 0);
            //lines.setEndPoint(10, 10, 0);
            //list.append(lines);
            //ln.setPrimitive(list);

            //ln = new UWB_SceneNode("Point Node");
            //mScene.insertChildNode(ln);
            //list = new UWB_PrimitiveList();
            //UWB_PrimitivePoint pt = new UWB_PrimitivePoint();
            //pt.setPointSize(20.0f);
            //pt.setLocation(0.0f, 0.0f, 0.0f);
            //list.append(pt);
            //pt = new UWB_PrimitivePoint();
            //pt.setPointSize(12.0f);
            //pt.setLocation(4.0f, 8.0f, 0.0f);
            //list.append(pt);
            //ln.setPrimitive(list);
            // End of the blocked out point and line





            mBullets = new UWB_PrimitiveList();
            mDrawHelper = new UWB_XNADrawHelper();
            mStopWatch = new UWB_Clock();
            mWorldBounds = new UWB_BoundingBox(new Vector3(-20, -20, 0), new Vector3(20, 20, 0));


            //////////////////////////////////////////////////////
            // Ok let's try to make some nets and moving balls
            // Here are the examples on how to make circles

            ////Make the square Arm2
            //UWB_PrimitiveRectangle Arm2Rec = new UWB_PrimitiveRectangle();
            //Arm2Rec.setCorners(new Vector3(16.0f, 1.0f, 0.0f), new Vector3(20.0f, 3.0f, 0.0f));
            //Arm2Rec.setFlatColor(new Color(00, 00, 00));
            //Arm2Rec.setShadeMode(eShadeMode.smFlat);
            //Arm2List.append(Arm2Rec);
            //LittleArm.setPrimitive(Arm2List);

            ////Make the Window
            //UWB_PrimitiveCircle Window = new UWB_PrimitiveCircle();
            //Window.setCenter(6.0f, 6.0f, 0.0f);
            //Window.setRadius(1.0f);
            //Window.setFlatColor(new Color(00, 255, 255, 0));
            //Window.setShadeMode(eShadeMode.smFlat);
            //SubBody.append(Window);

            //UWB_XFormInfo LAxf = LittleArm.getXFormInfo();
            //LAxf.setRotationDegrees(0.0f);
            //LAxf.setTranslation(new Vector3(0.0f, 0.0f, 0.0f));
            //LAxf.setPivot(new Vector3(16.0f, 2.0f, 0.0f));
            //LittleArm.setXFormInfo(LAxf);


            ////Make a Net
            //UWB_PrimitiveRectangle NetRec = new UWB_PrimitiveRectangle();
            //NetRec.setCorners(new Vector3(11.0f, 9.0f, 0.0f), new Vector3(15.0f, 13.0f, 0.0f));
            //NetRec.setFlatColor(new Color(00, 00, 00));
            //NetRec.setShadeMode(eShadeMode.smFlat);


            ////////////////////////////////////////////////

        }

        public UWB_SceneNode GetRootNode()
        {
            return mScene;
        }

        public void updateSimulation()
        {
            float elapsed_seconds = mStopWatch.getSecondsElapsed();

            //check for collisions between all of the existing balls and the SceneNode object
            //Balls that hit the palms should bounce
            //Balls that hit body count as a hit

            int count = mBullets.count();
            eLevelofDetail lod = mDrawHelper.getLod();

            UWB_BoundingBox left_palm_bounds = new UWB_BoundingBox();
            UWB_BoundingBox right_palm_bounds = new UWB_BoundingBox();

            mDrawHelper.initializeModelTransform();
            mScene.getNodeBounds(mLeftPalmNode, ref left_palm_bounds, mDrawHelper, false);
            mScene.getNodeBounds(mRightPalmNode, ref right_palm_bounds, mDrawHelper, false);


///////////
            UWB_BoundingBox Scope_Net_Bounds = new UWB_BoundingBox();
            UWB_BoundingBox Arm_Target_Bounds = new UWB_BoundingBox();
            UWB_BoundingBox Arm_Net_Bounds = new UWB_BoundingBox();
            UWB_BoundingBox Scope_Target_Bounds = new UWB_BoundingBox();

            mScene.getNodeBounds(ScopeNetHolder, ref Scope_Net_Bounds, mDrawHelper, false);
            mScene.getNodeBounds(ArmWithTarget, ref Arm_Target_Bounds, mDrawHelper, false);
            mScene.getNodeBounds(ArmNetHolder, ref Arm_Net_Bounds, mDrawHelper, false);
            mScene.getNodeBounds(ScopeWithTarget, ref Scope_Target_Bounds, mDrawHelper, false);


////////////
            // walk backward through list so we can remove balls when a hit occurs
            int hits = mHitCount;
            for (int i = count - 1; i >= 0; i--)
            {
                UWB_Primitive testPrimitive = mBullets.primitiveAt(i);
                if (null == testPrimitive)
                    continue;

                testPrimitive.update(elapsed_seconds);

                UWB_BoundingVolume boundVolume = testPrimitive.getBoundingVolume(lod);
                if (null == boundVolume)
                {
                    mBullets.deletePrimitiveAt(i);
                    continue;
                }

////////////////
                if (Scope_Net_Bounds.intersects(boundVolume))
                {
                    if (!AnimateScope)
                    {
                        testPrimitive.collisionResponse(ScopeNetHolder.getPrimitive(), Scope_Net_Bounds.getCenter());
                        mHitCount++;
                        //Move the balls far away
                        testPrimitive.moveTo(999, 999);
                        continue;
                    }
                }
                if (Arm_Net_Bounds.intersects(boundVolume))
                {
                    if (!AnimateArm)
                    {
                        testPrimitive.collisionResponse(ArmNetHolder.getPrimitive(), Arm_Net_Bounds.getCenter());
                        mHitCount++;
                        //Move the balls far away, so they will be deleted
                        testPrimitive.moveTo(999, 999);
                        continue;
                    }
                }
                if (Arm_Target_Bounds.intersects(boundVolume))
                {
                    testPrimitive.collisionResponse(ArmWithTarget.getPrimitive(), Arm_Target_Bounds.getCenter());
                    // Update that they hit a target
                    AnimateArm = true;
                    continue;
                }
                if (Scope_Target_Bounds.intersects(boundVolume))
                {
                    testPrimitive.collisionResponse(ScopeWithTarget.getPrimitive(), Scope_Target_Bounds.getCenter());
                    //Update that they hit a target
                    AnimateScope = true;
                    continue;
                }
/////////////////
                //Check to see if the bullet is hitting a net target
                //For now this is very simple, but let's see if it works
                //if( boundVolume.containsPoint(ArmNet1Center) )
                //{
                //    mHitCount++;
                //    //Move the thing outside the world
                //    testPrimitive.moveTo(999.0f, 999.0f);
                //    continue;
                //}
                //if (boundVolume.containsPoint(ArmNet2Center))
                //{
                //    mHitCount++;
                //    //Move the thing outside the world
                //    testPrimitive.moveTo(999.0f, 999.0f);
                //    continue;
                //}
                //if (boundVolume.containsPoint(ArmNet4Center))
                //{
                //    mHitCount++;
                //    //Move the thing outside the world
                //    testPrimitive.moveTo(999.0f, 999.0f);
                //    continue;
                //}
                //if (boundVolume.containsPoint(ArmNet4Center))
                //{
                //    mHitCount++;
                //    //Move the thing outside the world
                //    testPrimitive.moveTo(999.0f, 999.0f);
                //    continue;
                //}

                //if (left_palm_bounds.intersects(boundVolume))
                //{
                //    testPrimitive.collisionResponse(mLeftPalmNode.getPrimitive(), left_palm_bounds.getCenter());
                //    mHitCount++;
                //    continue;
                //}
                //if (right_palm_bounds.intersects(boundVolume))
                //{
                //    testPrimitive.collisionResponse(mRightPalmNode.getPrimitive(), right_palm_bounds.getCenter());
                //    mHitCount++;
                //    continue;
                //}
            }
            if (hits != mHitCount)
            {
                String msg = "Hit Count = " + mHitCount;
                UWB_Utility.echoToStatusArea(msg);
            }
            //else
            //{
            //    String msg = "Location = " + ArmNet1Center.ToString();
            //    UWB_Utility.echoToStatusArea(msg);
            //}

            mSecondCounter += elapsed_seconds;
            if (mSecondCounter > 0.5f)
            {
                mSecondCounter = 0.0f;
								for (int i = 0; i < bulletFrequency; ++i)
								{
									CreateNewBullet();
								}
            }

            //destroy balls that are outside the world
            count = mBullets.count();
            for (int i = count - 1; i >= 0; i--)
            {
                UWB_Primitive aBullet = mBullets.primitiveAt(i);
                if (null == aBullet)
                    continue;

                UWB_BoundingVolume bV = aBullet.getBoundingVolume(lod);
                if (null == bV || !bV.intersects(mWorldBounds))
                    mBullets.deletePrimitiveAt(i);
            }

            //------------  now update the rotation of the two palms ...

            //UWB_XFormInfo xform = mLeftPalmNode.getXFormInfo();
            //float rotation = xform.getRotationInDegrees();
            //xform.setPivot(new Vector3(5.0f, 4.0f, 0.0f));
            //rotation += 10;
            //if (rotation > 360)
            //    rotation -= 360.0f;

            //xform.setRotationDegrees(rotation);
            //mLeftPalmNode.setXFormInfo(xform);

            //xform = mRightPalmNode.getXFormInfo();
            //rotation = xform.getRotationInDegrees();
            //xform.setPivot(new Vector3(5.0f, -4.0f, 0.0f));
            //rotation -= 15;
            //if (rotation > 360)
            //    rotation -= 360.0f;

            //xform.setRotationDegrees(rotation);
            //mRightPalmNode.setXFormInfo(xform);

            ////////////////////////////////////
            //Now let's update our Targets
            //UWB_XFormInfo xform = mLeftPalmNode.getXFormInfo();
            //float rotation = xform.getRotationInDegrees();
/////////////////////
            // Did our targets get hit?
            
            if (AnimateArm)
            {
                //get the arm Node
                UWB_XFormInfo AX_Arm = ArmWithTarget.getXFormInfo();
                Vector3 Squish = AX_Arm.getScale();
                Squish.X -= 0.01f;
                if (Squish.X < 0.1)
                {
                    Squish.X = 0.001f;
                }
                AX_Arm.setScale(Squish);

                ArmWithTarget.setXFormInfo(AX_Arm);

                AnimateArmCount++;
            }

            if (AnimateArmCount > 100)
            {
                AnimateArmCount = 0;
                AnimateArm = false;
            }

            if (AnimateScope)
            {
                //get the arm Node
                UWB_XFormInfo AX_Scope = ScopeWithTarget.getXFormInfo();
                Vector3 Squish = AX_Scope.getScale();
                Squish.X -= 0.01f;
                Squish.Y -= 0.01f;
                if (Squish.X < 0.1)
                {
                    Squish.X = 0.001f;
                }
                if (Squish.Y < 0.1)
                {
                    Squish.Y = 0.001f;
                }
                AX_Scope.setScale(Squish);

                ScopeWithTarget.setXFormInfo(AX_Scope);

                AnimateScopeCount++;
            }

            if (AnimateScopeCount > 200)
            {
                AnimateScopeCount = 0;
                AnimateScope = false;
            }

//////////////////////
        }

				public void setTranslation(float worldCoorX, float worldCoorY)
				{
					mScene.getXFormInfo().setTranslation(new Vector3(worldCoorX, worldCoorY, 0));
				}

        public void getWorldBounds(ref float width, ref float height)
        {
            width = mWorldBounds.width();
            height = mWorldBounds.height();
        }

        public UWB_BoundingBox getWorldBounds()
        {
            return mWorldBounds;
        }

        public void drawGrid(UWB_DrawHelper drawHelper, UWB_BoundingBox bounds)
        {
            Color gridColor = Color.Gray;
            Color axisColor = Color.Black;

            drawHelper.setColor1(gridColor);
            drawHelper.setColor1(gridColor);

            Vector3 minPoint = bounds.getMin();
            Vector3 maxPoint = bounds.getMax();

            float y = minPoint.Y;
            while (y < maxPoint.Y)
            {
                if (y == 0)
                {
                    drawHelper.setColor1(axisColor);
                    drawHelper.setColor2(axisColor);
                    drawHelper.drawLine(new Vector3(minPoint.X, y, 0), new Vector3(maxPoint.X, y, 0));
                    drawHelper.setColor1(gridColor);
                    drawHelper.setColor2(gridColor);
                }
                else
                {
                    drawHelper.setColor1(gridColor);
                    drawHelper.setColor2(gridColor);
                    drawHelper.drawLine(new Vector3(minPoint.X, y, 0), new Vector3(maxPoint.X, y, 0));
                }
                y += 5;
            }

            float x = minPoint.X;
            while (x < maxPoint.X)
            {
                if (x == 0)
                {
                    drawHelper.setColor1(axisColor);
                    drawHelper.setColor2(axisColor);
                    drawHelper.drawLine(new Vector3(x, minPoint.Y, 0), new Vector3(x, maxPoint.Y, 0));
                    drawHelper.setColor1(gridColor);
                    drawHelper.setColor2(gridColor);
                }
                else
                {
                    drawHelper.setColor1(gridColor);
                    drawHelper.setColor2(gridColor);
                    drawHelper.drawLine(new Vector3(x, minPoint.Y, 0), new Vector3(x, maxPoint.Y, 0));
                }
                x += 5;
            }
        }

        public UWB_BoundingBox getWorldBound()
        { return mWorldBounds; }

        public void drawModel()
        {
            mDrawHelper.initializeModelTransform();

            drawGrid(mDrawHelper, mWorldBounds);

            mDrawHelper.pushModelTransform();

            eLevelofDetail lod = mDrawHelper.getLod();
            mScene.draw(lod, mDrawHelper);

            mDrawHelper.popModelTransform();

            mBullets.draw(lod, mDrawHelper);
        }

        public void moveCharacterX(float x)
        {
            UWB_XFormInfo xform = mScene.getXFormInfo();
            Vector3 t = xform.getTranslation();
            t.X = x;
            xform.setTranslation(t);
            mScene.setXFormInfo(xform);
        }

        private void CreateNewBullet()
        {
            Vector3 minpt = mWorldBounds.getMin();
            Vector3 maxpt = mWorldBounds.getMax();
            float x = UWB_Utility.UWB_RandomNumber(minpt.X, maxpt.X);

            float r = UWB_Utility.UWB_RandomNumber(0.0f, 5.0f);
            UWB_Primitive obj;
            if (r > 2.0f)
                obj = new CBall(x, maxpt.Y, 0);
            else
                obj = new CPrimitiveArm(x, maxpt.Y, 0);

					Vector3 currentPos = obj.getLocation();
					//Vector3 pivotPos = mScene.getXFormInfo().getPivot();
                    // Let's get it to the Sub's pivot
                    Vector3 pivotPos = SubNode.getXFormInfo().getPivot();
                    obj.setVelocity(pivotPos - currentPos);

					mBullets.append(obj);

        }

        public void setActiveNode(UWB_SceneNode n)
        {
            mActiveNode = n;
        }
        public UWB_SceneNode getActiveNode()
        {
            return mActiveNode;
        }

        public UWB_DrawHelper getDrawHelper()
        {
            return mDrawHelper;
        }

				public void setBulletFrequency(int bulletFrequency)
				{
					bulletFrequency = this.bulletFrequency;
				}

    }
}

